package com.yilin.tms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.system.system.AppVersion;
import com.yilin.tms.system.mapper.AppVersionMapper;
import com.yilin.tms.system.service.IAppVersionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppVersionService extends ServiceImpl<AppVersionMapper, AppVersion> implements IAppVersionService {

    @Resource
    AppVersionMapper appUpLoadDao;


    //-------------------------APP资源版本 上传方法--------------------------

    @Override
    public PageData<AppVersion> getAPPLoadPage(int i, Integer limit, AppVersion appVersion) {
        return page(new PageData<>(i, limit), QueryUtil.andQueryWrapper(new QueryWrapper<>(),appVersion));
    }

    @Override
    public void addAPPLoad(AppVersion appUpload) {
        if (appUpload.getId() != null) {//更新，
            appUpload.setUpdateTime(new Date());
            appUpLoadDao.updateById(appUpload);
        } else {//保存
            appUpload.setCreateTime(new Date());
            appUpLoadDao.insert(appUpload);
        }
    }

    @Override
    public AppVersion getNewAppVersion(AppVersion.AppType appType, AppVersion.SoftwareType softwareType, String platId, Integer verCode) {
        QueryWrapper<AppVersion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_type",appType.index);
        queryWrapper.eq("software_type",softwareType.index);
        queryWrapper.eq("plat_id",platId);
        if (verCode!=null) queryWrapper.eq("version_code",verCode);
        queryWrapper.orderByDesc("create_time");
        List<AppVersion> appVersions = appUpLoadDao.selectList(queryWrapper);
        return ArrayUtil.first(appVersions);
    }
}
